package leetCode.q498;

public class Solution {
    public static boolean orrent = true;
    public static int r = 0;
    public static int c = 0;

    public int[] findDiagonalOrder(int[][] mat) {
        r = mat.length;
        c = mat[0].length;
        int[] arr = new int[r * c];

        int a = 0;
        int b = 0;

        for (int i = 0; i < r * c; i++) {
            //遍历
            arr[i] = mat[a][b];

//            change(a, b);
            if (Solution.orrent) {
                //向上 i-j+
                if (a - 1 >= 0 && b + 1 < c) {
                    a--;
                    b++;
                }else {
                    //掉头
                    Solution.orrent = !orrent;
                    if(b+1<c){//go to the right
                        b++;
                    }else {
                        a++;
                    }
                }
            } else {
                //向下 i+j-
                if (a + 1 <r && b - 1 >=0) {
                    a++;
                    b--;
                }else {
                    //掉头
                    Solution.orrent = !orrent;
                    if(a+1<r){
                        a++;
                    }else {
                        b++;
                    }
                }
            }
        }

        return arr;
    }

    public static void main(String[] args) {
        int[][] mat = new int[][]{{1,2},{3,4}};
        int[] arr = new Solution().findDiagonalOrder(mat);
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}